package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.pages.bean.ExerciseRecordListBean;
import com.shushan.pages.bean.LastExerciseRecordBean;
import com.shushan.shushanbao.R;
import com.shushan.util.DateUtils;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: ExerciseRecordListAdapter
 * @Desciption: //练习记录列表listview适配器
 * @author: zhangshihao
 * @date: 2018-04-23
 */
public class ExerciseRecordListAdapter extends RecyclerView.Adapter<ExerciseRecordListAdapter.ViewHolder> {

    private Context context;
    private List<ExerciseRecordListBean> exerciseRecordListBeans;
    private String practice_type;

    private OnMoreClickListener onMoreClickListener;
    private OnLastRecordClickListener onLastRecordClickListener;

    public ExerciseRecordListAdapter(Context context, List<ExerciseRecordListBean> exerciseRecordListBeans) {
        this.context = context;
        this.exerciseRecordListBeans = exerciseRecordListBeans;
    }

    public void setOnMoreClickListener(OnMoreClickListener listener) {
        this.onMoreClickListener = listener;
    }

    public void setOnLastRecordClickListener(OnLastRecordClickListener listener) {
        this.onLastRecordClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_exercise_record_list, null, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final ExerciseRecordListBean bean = exerciseRecordListBeans.get(position);

        holder.tvExerciseType.setText(ToolUtils.getExerciseTypeNameByTypeId(bean.type) + "记录");
        holder.tvExerciseCount.setText(String.valueOf(bean.answer_num));
        holder.tvAnswerCount.setText(String.valueOf(bean.exercise_num));
        holder.tvRightRate.setText(String.valueOf(getRightRate(bean.right_num, bean.exercise_num)));

        List<LastExerciseRecordBean> lastBeans = bean.lastBeans;
        switch (lastBeans.size()) {
            case 0:
                holder.llLastRecord1.setVisibility(View.INVISIBLE);
                holder.llLastRecord2.setVisibility(View.INVISIBLE);
                holder.llLastRecord3.setVisibility(View.INVISIBLE);
                break;
            case 1:
                holder.llLastRecord2.setVisibility(View.INVISIBLE);
                holder.llLastRecord3.setVisibility(View.INVISIBLE);
                break;
            case 2:
                holder.llLastRecord3.setVisibility(View.INVISIBLE);
                break;
        }
        if (lastBeans.size() > 0) {
            for (int i = 0; i < lastBeans.size(); i++) {
                final LastExerciseRecordBean lastBean = lastBeans.get(i);
                switch (i) {
                    case 0:
                        holder.tvMonthDay.setText(DateUtils.captureMonthDay(lastBean.create_time));
                        holder.tvTime.setText(DateUtils.captureTimeWithoutSecond(lastBean.create_time));
                        holder.tvRightCount.setText(String.valueOf(lastBean.right_count));
                        holder.tvExerciseNum.setText(String.valueOf(lastBean.exercises_count));
                        holder.llLastRecord1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (onLastRecordClickListener != null) {
                                    onLastRecordClickListener.onLastRecordClick(position + 1, lastBean);
                                }
                            }
                        });
                        break;
                    case 1:
                        holder.tvAnswerMonthDay.setText(DateUtils.captureMonthDay(lastBean.create_time));
                        holder.tvAnswerTime.setText(DateUtils.captureTimeWithoutSecond(lastBean.create_time));
                        holder.tvAnswerRightCount.setText(String.valueOf(lastBean.right_count));
                        holder.tvAnswerExerciseNum.setText(String.valueOf(lastBean.exercises_count));
                        holder.llLastRecord2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (onLastRecordClickListener != null) {
                                    onLastRecordClickListener.onLastRecordClick(position + 1, lastBean);
                                }
                            }
                        });
                        break;
                    case 2:
                        holder.tvRateMonthDay.setText(DateUtils.captureMonthDay(lastBean.create_time));
                        holder.tvRateTime.setText(DateUtils.captureTimeWithoutSecond(lastBean.create_time));
                        holder.tvRateRightCount.setText(String.valueOf(lastBean.right_count));
                        holder.tvRateExerciseNum.setText(String.valueOf(lastBean.exercises_count));
                        holder.llLastRecord3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (onLastRecordClickListener != null) {
                                    onLastRecordClickListener.onLastRecordClick(bean.type, lastBean);
                                }
                            }
                        });
                        break;
                }
            }
        }
        holder.llMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMoreClickListener != null) {
                    onMoreClickListener.onClick(String.valueOf(bean.type));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return exerciseRecordListBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvExerciseType;
        private LinearLayout llMore;

        private TextView tvExerciseCount;
        private TextView tvMonthDay;
        private TextView tvTime;
        private TextView tvRightCount;
        private TextView tvExerciseNum;

        private TextView tvAnswerCount;
        private TextView tvAnswerMonthDay;
        private TextView tvAnswerTime;
        private TextView tvAnswerRightCount;
        private TextView tvAnswerExerciseNum;

        private TextView tvRightRate;
        private TextView tvRateMonthDay;
        private TextView tvRateTime;
        private TextView tvRateRightCount;
        private TextView tvRateExerciseNum;

        private LinearLayout llLastRecord1;
        private LinearLayout llLastRecord2;
        private LinearLayout llLastRecord3;

        private LinearLayout llParentLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            tvExerciseType = itemView.findViewById(R.id.tv_exercise_type);
            llMore = itemView.findViewById(R.id.ll_more);

            tvExerciseCount = itemView.findViewById(R.id.tv_exercise_count);
            tvMonthDay = itemView.findViewById(R.id.tv_month_day);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvRightCount = itemView.findViewById(R.id.tv_right_count);
            tvExerciseNum = itemView.findViewById(R.id.tv_exercise_num);

            tvAnswerCount = itemView.findViewById(R.id.tv_answer_count);
            tvAnswerMonthDay = itemView.findViewById(R.id.tv_answer_month_day);
            tvAnswerTime = itemView.findViewById(R.id.tv_answer_time);
            tvAnswerRightCount = itemView.findViewById(R.id.tv_answer_right_count);
            tvAnswerExerciseNum = itemView.findViewById(R.id.tv_answer_exercise_num);

            tvRightRate = itemView.findViewById(R.id.tv_right_rate);
            tvRateMonthDay = itemView.findViewById(R.id.tv_rate_month_day);
            tvRateTime = itemView.findViewById(R.id.tv_rate_time);
            tvRateRightCount = itemView.findViewById(R.id.tv_rate_right_count);
            tvRateExerciseNum = itemView.findViewById(R.id.tv_rate_exercise_num);

            llLastRecord1 = itemView.findViewById(R.id.ll_last_record_1);
            llLastRecord2 = itemView.findViewById(R.id.ll_last_record_2);
            llLastRecord3 = itemView.findViewById(R.id.ll_last_record_3);
            llParentLayout = itemView.findViewById(R.id.ll_parent_layout);
        }
    }

    private int getRightRate(int rightNum, int answerNum) {
        float rate = (float) rightNum / answerNum * 100;
        return (int) rate;
    }

    public interface OnMoreClickListener {
        void onClick(String practice_type);
    }

    public interface OnLastRecordClickListener {
        void onLastRecordClick(int practiceType, LastExerciseRecordBean lastBean);
    }

}
